package com.ruoyi.finance.service.impl;

import com.ruoyi.caigou.api.RemoteBuyOrderService;
import com.ruoyi.caigou.domain.BuyOrder;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.finance.domain.BuyPayReqRefCgrk;
import com.ruoyi.finance.domain.Payment;
import com.ruoyi.finance.domain.PaymentDetail;
import com.ruoyi.finance.mapper.BuyPayReqMapper;
import com.ruoyi.finance.mapper.BuyPayReqRefCgrkMapper;
import com.ruoyi.finance.mapper.PaymentDetailMapper;
import com.ruoyi.finance.mapper.PaymentMapper;
import com.ruoyi.finance.service.IAdvancePaymentService;
import com.ruoyi.finance.service.IPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 预付款单Service业务层处理
 *
 * @author lvql
 * @date 2024-11-08
 */
@Service
public class AdvancePaymentServiceImpl implements IAdvancePaymentService {
    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private PaymentDetailMapper paymentDetailMapper;

    @Autowired
    private BuyPayReqMapper buyPayReqMapper;

    @Autowired
    private BuyPayReqRefCgrkMapper buyPayReqRefCgrkMapper;


    @Autowired
    private RemoteBuyOrderService remoteBuyOrderService;

    /**
     * 查询付款单
     *
     * @param id 付款单主键
     * @return 付款单
     */
    @Override
    public Payment selectPaymentById(Integer id) {
        Payment payment = paymentMapper.selectPaymentById(id);
        String code = payment.getCode();
        PaymentDetail paymentDetail = new PaymentDetail();
        paymentDetail.setPaymentCode(code);
        List<PaymentDetail> paymentDetails = paymentDetailMapper.selectPaymentDetailList(paymentDetail);
        payment.setList(paymentDetails);
        return payment;
    }

    /**
     * 查询付款单列表
     *
     * @param payment 付款单
     * @return 付款单
     */
    @Override
    public List<Payment> selectPaymentList(Payment payment) {
        return paymentMapper.selectPaymentList(payment);
    }

    /**
     * 新增付款单
     *
     * @param payment 付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPayment(Payment payment) {
        payment.setCreateTime(DateUtils.getNowDate());
        List<PaymentDetail> list = payment.getList();
        BigDecimal bigDecimal = new BigDecimal(0);
        if (list != null && list.size() > 0) {
            for (PaymentDetail paymentDetail : list) {
                paymentDetail.setPaymentCode(payment.getCode());
                bigDecimal = bigDecimal.add(paymentDetail.getTotalAmount());
                paymentDetailMapper.insertPaymentDetail(paymentDetail);
            }
        }
        payment.setCodeType(12);
        payment.setTotalAmount(bigDecimal);
        payment.setVerifiedAmount(BigDecimal.ZERO);
        payment.setDocMakeUserId(Math.toIntExact(SecurityUtils.getUserId()));
        payment.setSourceCode(list.get(0).getSourceCode());
        return paymentMapper.insertPayment(payment);
    }

    /**
     * 修改付款单
     *
     * @param payment 付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePayment(Payment payment) {
        payment.setUpdateTime(DateUtils.getNowDate());
        List<PaymentDetail> list = payment.getList();
        BigDecimal bigDecimal = new BigDecimal(0);
        for (PaymentDetail paymentDetail : list) {
            bigDecimal = bigDecimal.add(paymentDetail.getTotalAmount());
            paymentDetailMapper.updatePaymentDetail(paymentDetail);
        }
        payment.setTotalAmount(bigDecimal);
        return paymentMapper.updatePayment(payment);
    }

    @Transactional
    @Override
    public int approvalPayment(Payment payment) {
        if (payment.getTotalAmount().intValue() == 0) {
            return paymentMapper.updatePayment(payment);
        }
        R<BuyOrder> infoByCode = remoteBuyOrderService.getInfoByCode(payment.getSourceCode(), SecurityConstants.INNER);
        if (R.FAIL == infoByCode.getCode()) {
            throw new ServiceException("查询订单远程调用失败,审核失败");
        }
        BuyOrder buyOrder = infoByCode.getData();
        BigDecimal add = buyOrder.getAdvanceAmount().add(payment.getTotalAmount());
        System.out.println("BigDecimal" + add);
        buyOrder.setAdvanceAmount(buyOrder.getAdvanceAmount().add(payment.getTotalAmount()));
        R<Boolean> booleanR = remoteBuyOrderService.innerEdit(buyOrder, SecurityConstants.INNER);
        if (R.FAIL == booleanR.getCode()) {
            throw new ServiceException("更新订单远程调用失败,审核失败");
        }
        payment.setApprovalUserId(Math.toIntExact(SecurityUtils.getUserId()));
        return paymentMapper.updatePayment(payment);
    }

    /**
     * 批量删除付款单
     *
     * @param ids 需要删除的付款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaymentByIds(Integer[] ids) {
        List<Payment> payments = paymentMapper.selectPaymentByIds(ids);
        List<String> codeList = new ArrayList<>();
        for (Payment payment : payments) {
            codeList.add(payment.getCode());
        }
        if (!codeList.isEmpty()) {
            paymentDetailMapper.deletePaymentDetailByPaymentCode(codeList);
        }
        return paymentMapper.deletePaymentByIds(ids);
    }

    /**
     * 删除付款单信息
     *
     * @param id 付款单主键
     * @return 结果
     */
    @Override
    public int deletePaymentById(Integer id) {
        return paymentMapper.deletePaymentById(id);
    }


}
